package offer.nowcoder.queue_stack;

import java.util.Stack;

/**
 * @author DengYuan2
 * @create 2021-02-16 20:45
 */
public class n_31 {
    public static void main(String[] args) {
        int[] in = new int[]{1, 2, 3, 4, 5};
//        int[] out = {4, 5, 3, 2, 1};
        int[] out = {4, 3,5,1,2};
        boolean res = IsPopOrder(in, out);
        System.out.println(res);
    }

    /**
     * 我的写法-和大神思路一致，已经照着修改成更简洁的形式了
     * @param pushA
     * @param popA
     * @return
     */
    public static boolean IsPopOrder(int[] pushA, int[] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            while (!stack.isEmpty() &&stack.peek() == popA[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }

}
